package my.DAL;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import my.Common.DBType;
import my.Common.clsConstant;
import my.Common.clsUtil;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

public class clsDal extends SQLiteOpenHelper {
	private SQLiteDatabase m_database;
	private Boolean m_blnStream;
	private Context m_context;
	private String m_strDatabasePath;
	
	public clsDal(Context context)
	{
		super(context, clsConstant.DATABASE_NAME, null, 1);
		m_context = context;
		m_blnStream = false;		
		m_strDatabasePath = context.getDatabasePath(clsConstant.DATABASE_NAME).getAbsolutePath();
	}
	
	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}	
	
	private void OpenDatabase()
	{
		if(m_database == null || !m_database.isOpen())
		{
			m_database = SQLiteDatabase.openDatabase(m_strDatabasePath, null, SQLiteDatabase.OPEN_READWRITE);
		}		
	}
	
	private void CloseDatabase()
	{
		if(!m_blnStream)
		{
			m_database.close();
		}		
	}
	
	public void InitializeDatabase() throws Exception
	{
		InputStream is;
    	OutputStream os;
    	byte[] buffer;
    	int length;    	    
    	
    	File fileDB = m_context.getDatabasePath(clsConstant.DATABASE_NAME);
    	if(!fileDB.exists())
    	{
    		this.getWritableDatabase();    		
    		is = m_context.getAssets().open(clsConstant.DATABASE_NAME);
    		os = new FileOutputStream(fileDB.getAbsoluteFile());
    		buffer = new byte[1024];
    		
    		while ((length = is.read(buffer))>0)
    		{
    			os.write(buffer, 0, length);
    		}
    		
    		os.flush();
    		os.close();
    		is.close();    	    	
    	}
	}
	
	public void BeginStream()
	{
		m_blnStream = true;
	}
	
	public void EndStream()
	{
		m_blnStream = false;
		if(m_database.isOpen())
		{
			m_database.close();
		}
	}
	
	public void ExecNonQuery(String sql) throws Exception
	{		
		try
		{
			OpenDatabase();
			m_database.execSQL(ReplaceSpecialChar(sql));			
		}
		catch(Exception ex)
		{
			throw ex;
		}
		finally
		{
			CloseDatabase();
		}
	}
	
	public Cursor ExecQuery(String sql) throws Exception
	{		
		Cursor cursor;
		try
		{
			OpenDatabase();
			cursor = m_database.rawQuery(ReplaceSpecialChar(sql), null);			
			cursor.moveToFirst();
			
			return cursor;
		}
		catch(Exception ex)
		{
			throw ex;
		}
		finally
		{
			CloseDatabase();
		}
	}	
	
	public void AddInParameter(String[] sql, String parm, DBType type, Object var) throws Exception
	{
		switch(type)
		{
			case String:
				sql[0] = sql[0].replace(parm + " ", "'" + var.toString().replace("'", "''") + "'" + " ");
				break;
			case DateTime:								
				sql[0] = sql[0].replace(parm + " ", "'" + clsUtil.FormatDate((Date) var) + "'" + " ");				
				break;
			case Boolean:				
				sql[0] = sql[0].replace(parm + " ", ((Boolean)var)== true? "1" : "0" + " ");				
				break;
			default:				
				sql[0] = sql[0].replace(parm + " ", var.toString() + " ");				
				break;
		}
	}
	
	private String ReplaceSpecialChar(String value)
	{
		return value.replace("%%", "%");
	}		
}
